package com.intretech.umsin.oss.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.intretech.umsin.oss.entity.FileInfoEntity;
import com.intretech.umsin.oss.param.FileInfoParam;
import com.intretech.umsin.oss.service.FileInfoService;
import com.intretech.umsin.oss.service.MetadataStoreService;
import com.intretech.umsin.oss.vo.FileInfoVo;
import com.intretech.umsin.oss.vo.converter.FileInfoVoEntityConverter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * @version V1.0
 * @ClassName: MetadataStoreDBServiceImpl
 * @Description: MetadataStoreDBServiceImpl 件元信息存储服务 数据库存储实现
 * @author: 李启联
 * @date: 2022-04-24 15:34
 * @Copyright: Copyright (c) 2022
 */
@Service
public class MetadataStoreDBServiceImpl implements MetadataStoreService {

    @Autowired(required = false)
    private FileInfoService fileInfoService;

    @Autowired(required = false)
    private FileInfoVoEntityConverter fileInfoVoEntityConverter;

    /**
     * 存储文件元信息
     *
     * @param fileInfo
     * @return
     */
    @Override
    public FileInfoVo save(FileInfoVo fileInfo) {
        return fileInfoService.add(fileInfo);
    }

    /**
     * 根据文件元信息ID删除文件元信息
     *
     * @param id 文件元信息ID
     * @return
     */
    @Override
    public Boolean delete(String id) {
        return fileInfoService.removeById(id);
    }

    /**
     * 根据ID 得到文件元信息
     *
     * @param id
     * @return
     */
    @Override
    public FileInfoVo findById(String id) {
        return fileInfoService.queryById(id);
    }

    /**根据fileMd5 得到文件元信息
     * @param fileMd5
     * @return
     */
    @Override
    public FileInfoVo findByFileMd5(String fileMd5) {
        List<FileInfoEntity> list = fileInfoService.list(new QueryWrapper<FileInfoEntity>().lambda().eq(FileInfoEntity::getFileMd5, fileMd5));
        if(!CollectionUtils.isEmpty(list)){
            return fileInfoVoEntityConverter.reverseFromTarget(list.get(0));
        }
        return null;
    }

    /**
     * 根据关联的业务ID查找对应的所有文件信息
     *
     * @param busId 关联的业务ID
     * @return
     */
    @Override
    public List<FileInfoVo> findByBusId(String busId) {
        List<FileInfoEntity> list = fileInfoService.list(new QueryWrapper<FileInfoEntity>().lambda().eq(FileInfoEntity::getBusinessId, busId));
        return fileInfoVoEntityConverter.reverseFromTarget(list);
    }

    /**
     * 根据关联的业务ID删除文件元信息
     *
     * @param busId
     * @return
     */
    @Override
    public Boolean deleteByBusId(String busId) {
      return  fileInfoService.remove(new QueryWrapper<FileInfoEntity>().lambda().eq(FileInfoEntity::getBusinessId, busId));
    }

    /**
     * 根据查询条件查询文件元信息
     *
     * @param param    查询条件参数
     * @param pageNo   当前页码
     * @param pageSize 每页条数
     * @return 自定义分页对象
     */
    @Override
    public IPage<FileInfoVo> findByPageResult(FileInfoParam param, Integer pageNo, Integer pageSize) {
      return fileInfoService.queryForPage( new Page(pageNo,pageSize),param);
    }
}
